define(function(require,exports,module) {
    var myPub = require('./public');

	var oBody = $('body');
    var sMemberLogin = $('.js_memberLoginBox').html();
    var sendTime = 60;


    //提示框
    function TipLayer (sCont,sStyle,fn,endFn) {
        layer.open({
            content: sCont,
            style:sStyle || 'background-color:rgba(255,255,255,1); color:#252525; border:none; border-radius: 5px; min-width:50%',
            scrollbar: false,
            time: 2,
            shadeClose: false,
            success:function() {if(fn)fn();},
            end:function () { if(endFn) endFn();}
        });
    }

    if (isBunded == '0'){
        loginLayer();
    }
    // loginLayer();
    // $('.js_showLayerMod').on('click',function (object) {
    //     loginLayer();
    // });

    function loginLayer() {

        //会员登录框显示
        myPub.TipBtnLayer(sMemberLogin,'绑定手机号','确定',function() {
            var oLayermcont = $('.layermcont');
            var oLoginPhoneNum = oLayermcont.find('.js_loginPhoneNum');			    // 手机号码框
            var oLoginCode = oLayermcont.find('.js_loginCode');			            // 验证码框

            //点击登录回调
            ajaxBindLogin(oLoginPhoneNum.val(),oLoginCode.val());
        },function() {
            //点击关闭按钮回调
			oBody.removeClass('addFixed');

        },function() {
            //出现登录框回调
            MemberLoginVer();
            ClearInput();
            $('.laymshade').on('touchmove',function() {return false});
			oBody.addClass('addFixed');
        })
    }
    exports.loginLayer = loginLayer;
    //登录验证
    function MemberLoginVer() {
        var oLayermcont = $('.layermcont');

        /******************   账号验证 start        ***********************/
        var oLoginPhoneNum = oLayermcont.find('.js_loginPhoneNum');			// 手机号码框
        var oGetCode = oLayermcont.find('.js_getCode');						// 验证码按钮
        var oGetCodeDisabled = 	oLayermcont.find('.js_getCodeDisabled');	// 验证码倒计时
        var oErrorTip_p = oLayermcont.find('.js_errorTip p');					// 错误提示


        //手机输入框
        oLoginPhoneNum.on('input propertychange',function(){
            phonemobile();
            var _oClearBtn = $(this).siblings('.js_clearBtn');
            $(this).val() == '' ? _oClearBtn.hide() : _oClearBtn.show();
        });

        //验证码
        oGetCode.hammer().on('tap',function(){
            var oLoginMob = oLoginPhoneNum.parents('.js_loginMob');

            if(oLoginPhoneNum.val() == '') {
                oErrorTip_p.text('请输入手机号！')
            }
            else if(oLoginMob.hasClass('has_error')) {
                oErrorTip_p.text('请输入手机号！')
            }
            else {
                $('#phone_number').attr('value',oLoginPhoneNum.val());
                ajaxSendCode(oLoginPhoneNum.val());
            }

        });

        /*验证手机号码*/
        function phonemobile() {
            var oLoginMob = oLoginPhoneNum.parents('.js_loginMob');
            myPub.checkMobile(oLoginPhoneNum,function() {
                //oLoginMob.removeClass('has_error').removeClass('has_success');
                oErrorTip_p.text('手机号码不能为空');
            },function() {
                //oLoginMob.addClass('has_error').removeClass('has_success');
                oErrorTip_p.text('手机号码有误,请重新填写');

            },function() {
                //oLoginMob.addClass('has_success').removeClass('has_error');
                oErrorTip_p.text('')
            });
        }

        /*倒计时*/
        function setCode() {
            oGetCode.hide();
            oGetCodeDisabled.show();
            myPub.CutTime(sendTime,function(){
                oGetCodeDisabled.hide();
                oGetCode.show();
            },function(time){
                oGetCodeDisabled.text('重新获取'+time+'s');
            });
        }

        function ajaxSendCode(mobile) { 
            $.ajax({
                url: _base + "/member/sendCode",
                data: {mobile: mobile},
                type: 'POST',
                dataType: "json",
                success: function (data) {
                    if (data.status == "success") {
                        setCode();
                    } else {
                        TipLayer(data.msg,null,null,function () {
                            loginLayer();
                        });
                    }
                }, error: function (x) {

                }
            });
        }
    }

    function ajaxBindLogin(mobile,code) {
        if (mobile == ''){
            TipLayer('请输入正确的手机号',null,null,function () {
                loginLayer();
            });
            return;
        }
        if (code == ''){
            TipLayer('请输入正确的验证码',null,null,function () {
                loginLayer();
            });
            return;
        }
        $.ajax({
            url: _base + "/member/bing",
            data: {mobile: mobile,verCode: code},
            type: 'POST',
            dataType: "json",
            success: function (data) {
                if (data.status == "success") {
                    // myPub.TipLayer(data.message);
                    TipLayer("绑定成功",null,null,function () {
                        if(isBound == 'new') {

                        }else {
                            window.location.href = '/myInfo/toMyInfo';
                        }

                    });


                } else {
                    // myPub.TipLayer(data.message);
                    TipLayer(data.msg,null,null,function () {
                        loginLayer();
                    });
                }
            }, error: function (x) {
            }
        });
    }


    function ClearInput() {
        var oClearBtn = $('.js_clearBtn');
        // 清空输入内容  oSearchClear
        oClearBtn.on('click',function(e) {

            $(this).hide().siblings('.js_Text').val('').siblings('.js_error').hide().removeClass('on');
            e.stopPropagation();
        });
    }


});